﻿using GadgetStore.Data;
using GadgetStore.Data.Models;
using GadgetStore.Web.Adapters.Interfaces;
using GadgetStore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GadgetStore.Web.Adapters
{
    public class PictureAdapter :IPicture
    {
        public List<Picture> GetAllPicts()
        {
            ApplicationDbContext Db = new ApplicationDbContext();
            return Db.Pictures.ToList();
        }

        public int UpdatePict(Picture picture)
        {
            ApplicationDbContext Db = new ApplicationDbContext();
            Picture EditedPicture = Db.Pictures.Where(p => p.Id == picture.Id).FirstOrDefault();
            EditedPicture.PictUrl = picture.PictUrl;
            Db.SaveChanges();
            return picture.GadgetId;
        }

        public int DeletePict(int id)
        {
            ApplicationDbContext Db = new ApplicationDbContext();
            Picture Picture = Db.Pictures.Where(p => p.Id == id).FirstOrDefault();
            Db.Pictures.Remove(Picture);
            Db.SaveChanges();
            return Picture.GadgetId;
        }


        public int NewPict(GadgetVM vm)
        {
            ApplicationDbContext Db = new ApplicationDbContext();
            Picture NewPicture = new Picture();
            NewPicture.PictUrl = vm.Picture.PictUrl;
            NewPicture.GadgetId = vm.Gadget.Id; 
            Db.Pictures.Add(NewPicture);
            Db.SaveChanges();
            return NewPicture.GadgetId;
        }
        public Picture GetOnePict(int id)
        {
            ApplicationDbContext Db = new ApplicationDbContext();
            return Db.Pictures.Where(p => p.Id == id).FirstOrDefault();
        }
    }
}